package com.ug.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ug.common.id.SnowFlakeFactory;
import com.ug.common.utils.*;
import com.ug.system.entity.SysLoginLog;
import com.ug.system.entity.SysOperLog;
import com.ug.system.entity.SysUser;
import com.ug.system.entity.dto.QueryDto;
import com.ug.system.entity.dto.SysLoginLogDto;
import com.ug.system.mapper.SysLoginLogMapper;
import com.ug.system.service.SysLoginLogService;
import com.ug.system.service.SysUserService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 系统访问记录 服务实现类
 * @author LY
 * @since 2024-08-29 16:35:21
 */
@Service
@RequiredArgsConstructor
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLoginLog> implements SysLoginLogService {

    private final SysUserService sysUserService;

    @Override
    public void saveLoginLog(SysLoginLog loginLog) {
        //  请求对象
        HttpServletRequest request = RequestHolder.getHttpServletRequest();
        //  获取客户端浏览器名称
        String browser = UserAgentUtils.getBorderName(request);
        //  获取客户端操作系统
        String os = UserAgentUtils.getOs(request);
        //  获取设备名称
        String deviceType = UserAgentUtils.deviceName(request);
        //  获取ip
        String ip = IpUtils.getIpAddress(request);

        long userId = Objects.requireNonNull(SecurityUtil.getCurrentUserId());

        loginLog.setLoginId(SnowFlakeFactory.getSnowFlakeFromCache().nextId());
        loginLog.setUserId(userId);
        loginLog.setUserName(SecurityUtil.getCurrentUsername());
        loginLog.setIpaddr(ip);
        loginLog.setLoginLocation(IpRegionUtil.getInstance().getRegion(ip));
        loginLog.setBrowser(browser);
        loginLog.setOs(os);
        loginLog.setDeviceType(deviceType);
        loginLog.setLoginTime(LocalDateTime.now());

        if (StrUtil.isEmpty(loginLog.getUserName())){
            //  处理使用快捷登录默认初始用户名为空时使用昵称
            SysUser sysUser = sysUserService.findByUserId(userId);
            if (sysUser != null && StrUtil.isEmpty(sysUser.getUsername())) {
                loginLog.setUserName(sysUser.getNickName());
            }
        }

        baseMapper.insert(loginLog);
    }

    @Override
    public IPage<SysLoginLog> getLoginLogList(QueryDto queryDto, SysLoginLogDto sysLoginLogDto) {
        LambdaQueryWrapper<SysLoginLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(SysLoginLog::getLoginTime);
        if (sysLoginLogDto.getStatus() != null){
            wrapper.eq(SysLoginLog::getStatus, sysLoginLogDto.getStatus());
        }
        if (StringUtil.isNotBlank(sysLoginLogDto.getUserName())){
            wrapper.eq(SysLoginLog::getUserName, sysLoginLogDto.getUserName());
        }
        if (StringUtil.isNotBlank(sysLoginLogDto.getDescription())){
            wrapper.eq(SysLoginLog::getDescription, sysLoginLogDto.getDescription());
        }
        IPage<SysLoginLog> page = new Page<>();
        page.setSize(queryDto.getSize());
        page.setCurrent(queryDto.getCurrentPage());
        return baseMapper.selectPage(page, wrapper);
    }
}
